package com.zhentao.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhentao.file.service.Impl.FileStorageServiceImpl;
import com.zhentao.mapper.WmMaterialMapper;
import com.zhentao.model.dtos.PageResponseResult;
import com.zhentao.model.dtos.ResponseResult;
import com.zhentao.model.enums.AppHttpCodeEnum;
import com.zhentao.model.wemedia.dto.WmMaterialDto;
import com.zhentao.model.wemedia.pojo.WmMaterial;
import com.zhentao.service.WmMaterialService;
import com.zhentao.utils.common.WmUserThreadLocalUtil;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;

/**
 * @author 桃
 */
@Service
public class WmMaterialServiceImpl extends ServiceImpl<WmMaterialMapper, WmMaterial>
        implements WmMaterialService {

    @Autowired
    private WmMaterialMapper materialMapper;

    @Autowired
    private FileStorageServiceImpl fileStorageService;


    /**
     * 图片上传
     */
    @SneakyThrows
    @Override
    public ResponseResult uploadPicture(MultipartFile multipartFile) {

        //1校验参数
        if (multipartFile == null || multipartFile.getSize() <= 0) {
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //把文件上传到minio里面
        String url = fileStorageService.uploadImageFile(multipartFile.getOriginalFilename(), multipartFile.getInputStream());

        //把数据保持到数据库里面
        WmMaterial wmMaterial = new WmMaterial();
        wmMaterial.setCreatedTime(new Date());
        wmMaterial.setIsCollection((short) 0);
        wmMaterial.setUserId(WmUserThreadLocalUtil.getUser().getId());
        //图片类型 可以在后台进行判断出来
        wmMaterial.setType((short) 0);

        //图片路劲
        wmMaterial.setUrl(url);

        materialMapper.insert(wmMaterial);

        return ResponseResult.okResult(wmMaterial);
    }

    /**
     * 列表
     */

    @Override
    public ResponseResult findList(WmMaterialDto materialDto) {

        //检查参数
        materialDto.checkParam();
        //分页
        IPage page = new Page(materialDto.getPage(), materialDto.getSize());

        //根据用户Id查询
        LambdaQueryWrapper<WmMaterial> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(WmMaterial::getUserId, WmUserThreadLocalUtil.getUser().getId());
        //排序    应该根据id来排序    能不能根据创建时间来排序  ？
        //  修改时间  1  2   3
        lambdaQueryWrapper.orderByDesc("id".isEmpty());
        //是否收藏
        lambdaQueryWrapper.eq(materialDto.getIsCollection() != 0,
                WmMaterial::getIsCollection, materialDto.getIsCollection());

        //执行查询操作
        page = materialMapper.selectPage(page, lambdaQueryWrapper);
        //3返回结果
        PageResponseResult responseResult = new PageResponseResult();
        responseResult.setCode(200);
        responseResult.setErrorMessage("查询成功");
        responseResult.setSize((int) page.getSize());
        responseResult.setCurrentPage((int) page.getCurrent());
        responseResult.setTotal((int) page.getTotal());
        responseResult.setData(page.getRecords());

        return responseResult;

    }

    /**
     * 收藏
     */
    @Override
    public ResponseResult collect(Integer id) {
        WmMaterial wmMaterial = materialMapper.selectById(id);
        wmMaterial.setIsCollection((short) 1);
        materialMapper.updateById(wmMaterial);
        return ResponseResult.okResult("收藏成功");
    }

    @Override
    public ResponseResult cancelCollect(Integer id) {
        WmMaterial wmMaterial = materialMapper.selectById(id);
        wmMaterial.setIsCollection((short) 0);
        materialMapper.updateById(wmMaterial);
        return ResponseResult.okResult("取消收藏成功");
    }

    /**
     * 删除
     */
    @Override
    public ResponseResult delPicture(Integer id) {
        materialMapper.deleteById(id);
        return  ResponseResult.okResult("删除成功");
    }




}
